<?php
/**
 * Created by PhpStorm.
 * Author: kaodou
 * Date: 2017/10/18
 * Time: 14:33
 */
namespace App\Http\Admin\Stock\Models;

use Framework\BaseClass\Http\Admin\Model;
use App\Eloquent\Admin\Stock as EloquentStock;

class Stock extends Model
{
    public function getProList()
    {
        return EloquentStock::all();
    }

    public function addStock($saveData)
    {
        $StockInfo = EloquentStock::firstOrNew(['water_no' => $saveData['water_no']]);
        if ($StockInfo->id) xThrow(ERR_CLIENT_USER_ALREADY_EXISTS);

        if($this->saveStock($StockInfo, $saveData)){
            return $StockInfo->id;
        }else{
            return false;
        }
    }

    public function updateStock($StockId, $saveData)
    {
        $StockInfo = EloquentStock::find($StockId);
        if (!$StockInfo) xThrow(ERR_CLIENT_USER_NOT_EXIST);

        return $this->saveStock($StockInfo, $saveData);
    }

    private function saveStock(EloquentStock $StockInfo, array $saveData)
    {
        $StockInfo->fill($saveData);
        return $StockInfo->save();
    }
}